<template>
  <div class="review-topic">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true"/>
    <div class="edu-page-warp" v-show="indexKey == 1">
      <condition-card v-if="isAdmin" @reset="reset" @search="searchList" label-width="140px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="毕业年份">
              <el-date-picker
                class="search-item"
                :clearable="false"
                v-model="queryForm.condition.grade"
                @change="handleGradeChange"
                value-format="yyyy"
                type="year"
                placeholder="请选择"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行政学院">
              <el-select
                v-model="queryForm.condition.faculty"
                filterable
                @change="handleCollegeChange"
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.studentColleges"
                  :key="item.collegeId"
                  :label="item.collegeIdI18n"
                  :value="item.collegeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业">
              <el-select
                v-model="queryForm.condition.subject"
                filterable
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.subjectOptions"
                  :key="item.subjectId"
                  :label="item.subjectName"
                  :value="item.subjectId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题性质">
              <el-select
                v-model="queryForm.condition.subjectProperties"
                filterable
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.subjectNatureOptions"
                  :key="item.id"
                  :label="item.subjectProperties"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题名称">
              <el-input
                v-model="queryForm.condition.projectName"
                class="search-item"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="导师">
              <el-input
                v-model="queryForm.condition.teacherNum"
                class="search-item"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">查看任务书</div>
        <div class="edu-opertion-bar">
          <div class="float-left" v-if="isAdmin">
            <el-input
              v-model="queryForm.condition.studentName"
              clearable
              placeholder="输入学生学号、姓名搜索"
              style="width:200px"
              @keyup.enter.native="searchList"
            >
              <i
                slot="suffix"
                class="el-icon-search"
                style="margin-top:10px;cursor:pointer"
                @click="searchList"
              ></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button type="default" @click="exportFile()">{{$t('message.export')}}</el-button>
          </div>
        </div>
        <el-table
          :data="taskList"
          size="mini"
          style="width:100%"
          @selection-change="handleSelectChange"
          tooltip-effect="light"
        >
          <el-table-column type="selection" stripe/>
          <el-table-column label="毕业年份" fixed="left">
            <template slot-scope="scope">{{ scope.row.grade }}</template>
          </el-table-column>
          <el-table-column label="课题名称" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{scope.row.projectName}}</template>
          </el-table-column>
          <el-table-column label="课题性质" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.subjectProperties }}</template>
          </el-table-column>
          <el-table-column label="导师" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.teacherName }}</template>
          </el-table-column>
          <el-table-column label="学生" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.studentName }}</template>
          </el-table-column>
          <el-table-column label="行政学院" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.facultyI18n }}</template>
          </el-table-column>
          <el-table-column label="专业" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.subject }}</template>
          </el-table-column>
          <el-table-column label="方向" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.direction }}</template>
          </el-table-column>
          <el-table-column :label="$t('label.operation')" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="viewDetail(scope.row)"
              >{{$t("message.detail")}}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="edu-pagination">
          <div class="float-right">
            <el-pagination
              :current-page="queryForm.pageNum_"
              :page-sizes="[20,50,100,500,1000]"
              :page-size="queryForm.pageSize_"
              :total="queryForm.total"
              layout="total, sizes, prev, pager, next, jumper"
              style="float: right;margin:10px 60px 0 0;"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-card>
    </div>
    <div v-if="indexKey == 2">
      <TaskInfo
        ref="topicInfo"
        :studentInfo="studentInfo"
        :subjectType="subjectType"
        :actionType="actionType"
        :basicInfoForm="basicInfoForm"
      ></TaskInfo>
    </div>
  </div>
</template>
<script>
  import API from 'common/src/api/underGraduationDesign/reviewSubject'
  import {
    exportData,
    getExportStatus,
    getList,
    getSubjectType,
    viewData
  } from 'common/src/api/underGraduationDesign/docTaskBook'
  import tools from 'common/src/views/underGraduationDesign/tools.js';
  import mixin from 'common/src/mixins/index';
  export default {
    name: 'taskView',
    mixins: [mixin],
    data() {
      return {
        isAdmin: false,
        api: API,
        queryForm: {
          pageNum_: 1,
          pageSize_: 20,
          condition: {
            grade: null,
            faculty: null,
            teacherNum: null,
            subjectProperties: null,
            projectName: null,
            studentName: null,
            subject: null
          }
        },
        basicInfoForm: {},
        studentInfo: {},
        taskList: [],
        indexKey: 1,
        breadPath: {
          label: '查看任务书',
          key: '1',
          children: [
            {
              key: '2',
              label: '查看任务书'
            }
          ]
        },
        selectedData: [], // 勾选的列表数据
        actionType: '',
        subjectType: '',
        isEdit: false,
        currentStudent: '',
        progressData: [], //审核进度列表
        source: {
          studentColleges: [], // 行政学院
          subjectOptions: [], // 专业
          collegeOptions: [],
          projectList: []
        }
      }
    },
    mounted() {
      this.isAdmin = this.global.isAdmin;
      this.queryForm.condition.grade = new Date().getFullYear() + '';
      this.getCollege()
      this.getSubjectNature()
      this.searchList()
      tools.initAllCollege(this.source.studentColleges);
    },
    methods: {
      handleSizeChange(size) {
        this.queryForm.pageSize_ = size;
        this.searchList()
      },
      handleCurrentChange(pageNum) {
        this.queryForm.pageNum_ = pageNum;
        this.searchList()
      },
      searchList() {
        getList(this.queryForm).then(res => {
          if (res.code == 200) {
            this.taskList = res.data.list
            this.queryForm.total = res.data.total
          }
        })
      },
      // 查课题性质
      getSubjectNature() {
        const params = {
          year: this.queryForm.condition.grade
        }
        this.api
        .getNature(params)
        .then(res => {
          this.source.subjectNatureOptions = res.data
        })
        .catch()
      },
      reset() {
        this.queryForm = {
          pageNum_: 1,
          pageSize_: 20,
          condition: {
            grade: null,
            faculty: null,
            teacherNum: null,
            subjectProperties: null,
            projectName: null,
            studentName: null,
            subject: null
          }
        }
        this.source.subjectOptions = []
        this.queryForm.condition.grade = new Date().getFullYear() + '';
        this.getSubjectNature();
        this.searchList()
      },
      handleSelectChange(val) {
        this.selectedData = val
      },
      handleGradeChange(val) {
        this.queryForm.condition.subjectId = null
        this.source.subjectOptions = []
        if (val && this.queryForm.condition.faculty) {
          this.handleCollegeChange(this.queryForm.condition.faculty)
        }
        this.getSubjectNature();
        this.searchList();
      },
      // 查学院
      getCollege() {
        this.api.getCollege().then(res => {
          this.source.collegeOptions = res
        })
      },
      // 选择年份和学院查专业
      handleCollegeChange(val) {
        this.queryForm.condition.subjectId = ''
        this.source.subjectOptions = []
        if (val && this.queryForm.condition.grade) {
          const params = {
            grade: this.queryForm.condition.grade,
            college: val
          }
          this.api
          .getSubject(params)
          .then(res => {
            this.source.subjectOptions = res.data
          })
          .catch()
        }
      },
      viewDetail(row) {
        this.actionType = 'VIEW_STU';
        viewData(row.id).then(res => {
          this.basicInfoForm = res.data
          this.studentInfo = {
            studentId: res.data.studentId,
            studentName: res.data.studentName,
            faculty: res.data.facultyI18n,
            subject: res.data.subject,
            direction: res.data.direction
          }
        })
        getSubjectType(row.studentId).then(res => {
          this.subjectType = res.data
        })
        this.indexKey = "2";
      },
      exportFile() {
        var params = {
          vo: this.queryForm.condition,
          list: this.selectedData
        }
        exportData(params).then(res => {
          //this.delayRequest(res.key)
          tools.downloadFile(res.key, "查看任务书.zip", getExportStatus, this);
        })
      },
      // 查文件导出状态，等待导出
      async delayRequest(key) {
        this.loading = true
        await new Promise(resolve => setTimeout(resolve, 3000))
        getExportStatus(key)
        .then(res => {
          this.loading = false
          if (res.code === 200 && res.data.status) {
            //let path = 'api/designservice/common/export?path=' + res.data.path
            let path = 'api/designservice/resultPublish/downLoadMore?path=' + res.data.path
              + "&fileName=docTask.zip";
            window.open(path)
          } else {
            this.delayRequest(key)
          }
        })
        .catch(err => {
          this.loading = false
          this.delayRequest(key)
        })
      }
    },
    components: {
      TaskInfo: () => import('../components/taskInfo')
    }
  }
</script>

<style lang="scss" scoped>
  .review-topic {
    .search-item {
      width: 100%;
    }
  }
</style>
